// 动物图片模型
const { DataTypes } = require("sequelize");
const sequelize = require("../config/sequelize.js");
const Animal = require("./Animal.js");

const AnimalImage = sequelize.define("animal_image", {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    allowNull: false
  },
  animalId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    field: 'animal_id',
    references: {
      model: 'animals',
      key: 'id'
    }
  },
  imageUrl: {
    type: DataTypes.STRING(255),
    allowNull: false,
    field: 'image_url'
  },
  isExclusive: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
    field: 'is_exclusive'
  },
  createdAt: {
    type: DataTypes.DATE,
    allowNull: true
  }
}, {
  tableName: "animal_images",
  timestamps: true,
  indexes: [
    { fields: ['animal_id'] }
  ]
});

// 设置关联关系
AnimalImage.belongsTo(Animal, { foreignKey: 'animalId' });
Animal.hasMany(AnimalImage, { foreignKey: 'animalId' });

module.exports = AnimalImage;